﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SharpSoft.WPF.Masks
{
    /// <summary>
    /// 用于自定义弹出框内容的容器
    /// </summary>
    public class MaskContainer : ContentControl
    {
        public MaskContainer(Mask maskHost) : this()
        {
            MaskHost = maskHost;
        }
        public MaskContainer()
        {
            this.CommandBindings.Add(new CommandBinding(CloseCommand, new ExecutedRoutedEventHandler(CloseMask)));
        }
        public Mask MaskHost { get; internal set; }

        public static RoutedCommand CloseCommand = new RoutedCommand();


        void CloseMask(object sender, ExecutedRoutedEventArgs e)
        {
            this.MaskHost?.Close();
            OnMaskClosed(e.Parameter as bool?);
        }
        /// <summary>
        /// 关闭弹出的遮罩层
        /// </summary>
        /// <param name="result">对话结果</param>
        public void Close(bool? result = null)
        {
            CloseCommand.Execute(result, this);
        }

        /// <summary>
        /// 在弹出的遮罩层关闭时触发
        /// </summary>
        public event MaskClosedRoutedEventHandler MaskClosed
        {
            add { AddHandler(MaskClosedEvent, value); }
            remove { RemoveHandler(MaskClosedEvent, value); }
        }

        public static readonly RoutedEvent MaskClosedEvent = EventManager.RegisterRoutedEvent(
        "MaskClosed", RoutingStrategy.Bubble, typeof(MaskClosedRoutedEventHandler), typeof(MaskContainer));

        protected virtual void OnMaskClosed(bool? result)
        {
            MaskClosedRoutedEventArgs args = new MaskClosedRoutedEventArgs() { Result = result };
            args.RoutedEvent = MaskContainer.MaskClosedEvent;
            this.RaiseEvent(args);
        }

    }

    public delegate void MaskClosedRoutedEventHandler(object sender, MaskClosedRoutedEventArgs e);

    public class MaskClosedRoutedEventArgs : RoutedEventArgs
    {
        public bool? Result { get; set; }
    }
}
